<?php defined("MAIN_DIR") or die("No direct access");

to("/banks");

$rko_query = 
"select `R`.*,`B`.`link`,`B`.`name`,`B`.`parent`
from `rko` as `R`,`bank` as `B`
where 
	`R`.`bank_id` = `B`.`bank_id` and
	(
		(`R`.`regionally` = '1' and
			(
				`B`.`bank_id` in (select `parent` from `bank` where `city_id` = '$_SESSION[city]' and `parent` != '0' ) or
				`B`.`bank_id` in (select `bank_id` from `bank` where `city_id` = '$_SESSION[city]' and `parent` = '0' )
			)
		) or
		(`B`.`city_id` = '$_SESSION[city]' and `R`.`regionally` = '0')
	)
group by `R`.`rko_id`";
$rko_res = mysql_query($rko_query);

$mas = $new_arr = array();
while ($rko_row = mysql_fetch_array($rko_res)) {
	$mas['rko'][] = $rko_row['rko_id'];
	$mas['bank'][] = $rko_row['bank_id'];
	$mas['parent'][] = $rko_row['parent'];
}

if (mysql_num_rows($rko_res) != 0)
	for ($i = 0; $i < count($mas['bank']); $i++)
		for ($j = 0; $j < count($mas['parent']); $j++)
			if ($mas['bank'][$i] == $mas['parent'][$j])
				$new_arr[] = $mas['rko'][$i];

$rko_query .= ($str = implode(",",$new_arr)) ? " and `R`.`rko_id` not in ($str)" : "";

if (isset($_GET['sort']) && $_GET['sort'] != "" && isset($_GET['ord']) && $_GET['ord'] != "") {
	$sort = trim(strip_tags($_GET['sort']));
	$ord = trim(strip_tags($_GET['ord']));
	switch ($sort) {
		
		case "open": $rko_query .= ($ord == "desc") ? " order by `account_opening` desc" : " order by `account_opening`"; break;
		case "maintenance": $rko_query .= ($ord == "desc") ? " order by `account_maintenance_low` desc,`account_maintenance_high` desc" : " order by `account_maintenance_low`,`account_maintenance_high`"; break;
		case "banking": $rko_query .= ($ord == "desc") ? " order by `internet_banking_low` desc,`internet_banking_high` desc" : " order by `internet_banking_low`,`internet_banking_high`"; break;
		case "transfer": $rko_query .= ($ord == "desc") ? " order by `transfer_low` desc,`transfer_high` desc" : " order by `transfer_low`,`transfer_high`"; break;
		case "cash": $rko_query .= ($ord == "desc") ? " order by `cash_low` desc,`cash_high` desc" : " order by `cash_low`,`cash_high`"; break;
		case "receiving": $rko_query .= ($ord == "desc") ? " order by `receiving_cash_low` desc,`receiving_cash_high` desc" : " order by `receiving_cash_low`,`receiving_cash_high`"; break;
	}
} else {
	$rko_query .= " order by `B`.`name`";
}

$limit = 5;
$rko_res = mysql_query($rko_query);
$count = mysql_num_rows($rko_res);
$page = 1;
if (isset($_GET['page']))
	$page = $_GET['page'];
$max_page = ceil($count/$limit);
if ($max_page == 0)
	$max_page++;
if ($page > $max_page)
	$page = $max_page;
elseif ($page < 1)
	$page = 1;
$start_list = (ceil($page)-1)*$limit;
$rko_query .= " limit $start_list,$limit";
$rko_res = mysql_query($rko_query);
$rko_count = mysql_num_rows($rko_res);

$head_title = "Рассчетно-кассовое облсуживание (РКО) в ".$current_city['city_name_pred'].", открыть счет в ".$current_city['city_name_pred'];
$head_description = "Тарифы на РКО в ".$current_city['city_name_pred'];
$head_keywords = "РКО рассчетно кассовое обслуживание ".$current_city['city_name']." открыть счет";

require_once APP.HEADFILE;
?>
<script type="text/javascript" src="/files/js/bank_list.js"></script>
<h1>РКО в <?php echo $current_city['city_name_pred']; ?></h1><?php
if ($rko_count > 0)
{
	?><div id="rko_table">
		<table width="100%" border="0" cellspacing="0" cellpadding="6">
			<tr>
				<td width="25%">&nbsp;</td>
				<td width="12%" bgcolor="#a3a3a3" class="rko_legenda_white rko_left_border">Содержание&nbsp;счета</td>
				<td width="11%">&nbsp;</td>
				<td width="14%">&nbsp;</td>
				<td width="14%" bgcolor="#a3a3a3" class="rko_legenda_white rko_left_border">Операции&nbsp;с&nbsp;деньгами</td>
				<td width="12%">&nbsp;</td>
				<td width="12%">&nbsp;</td>
			</tr>
			<tr class="rko_legenda_black">
				<td bgcolor="#FFFFFF">Банк</td>
				<td bgcolor="#FFFFFF" class="rko_left_border">Открытие</td>
				<td bgcolor="#FFFFFF">Ведение</td>
				<td bgcolor="#FFFFFF">Интернет-банкинг</td>
				<td bgcolor="#FFFFFF"  class="rko_left_border">Перевод, руб.</td>
				<td bgcolor="#FFFFFF">Выдача, %</td>
				<td bgcolor="#FFFFFF">Прием, %</td>
			</tr>
			<tr>
				<td>&nbsp;</td>
				<td class="rko_left_border">
					<a href="?sort=open&ord=asc"><img src="/files/images/arr_table_top.png" width="7" height="7" border="0" /></a>
					<a href="?sort=open&ord=desc"><img src="/files/images/arr_table_bottom.png" height="7" border="0" /></a>
				</td>
				<td>
					<a href="?sort=maintenance&ord=asc"><img src="/files/images/arr_table_top.png" alt="" width="7" height="7" border="0" /></a>
					<a href="?sort=maintenance&ord=desc"><img src="/files/images/arr_table_bottom.png" alt="" height="7" border="0" /></a>
				</td>
				<td>
					<a href="?sort=banking&ord=asc"><img src="/files/images/arr_table_top.png" alt="" width="7" height="7" border="0" /></a>
					<a href="?sort=banking&ord=desc"><img src="/files/images/arr_table_bottom.png" alt="" height="7" border="0" /></a>
				</td>
				<td class="rko_left_border">
					<a href="?sort=transfer&ord=asc"><img src="/files/images/arr_table_top.png" alt="" width="7" height="7" border="0" /></a>
					<a href="?sort=transfer&ord=desc"><img src="/files/images/arr_table_bottom.png" alt="" height="7" border="0" /></a>
				</td>
				<td>
					<a href="?sort=cash&ord=asc"><img src="/files/images/arr_table_top.png" alt="" width="7" height="7" border="0" /></a>
					<a href="?sort=cash&ord=desc"><img src="/files/images/arr_table_bottom.png" alt="" height="7" border="0" /></a>
				</td>
				<td>
					<a href="?sort=receiving&ord=asc"><img src="/files/images/arr_table_top.png" alt="" width="7" height="7" border="0" /></a>
					<a href="?sort=receiving&ord=desc"><img src="/files/images/arr_table_bottom.png" alt="" height="7" border="0" /></a>
				</td>
			</tr><?php
			while ($rko_row = mysql_fetch_array($rko_res))
			{
				?><tr>
					<td><a href="<?php echo "/$c_name";?>/banks/<?php echo $rko_row['link'].".html"; ?>"><?php echo $rko_row['name']; ?></a></td>
					<td class="rko_table_items rko_left_border"><?php echo ($rko_row['account_opening'] == 0) ? "бесплатно" : $rko_row['account_opening']; ?></td>
					<td class="rko_table_items"><?php
						if ($rko_row['account_maintenance_low'] == 0) {
							if ($rko_row['account_maintenance_high'] == 0) {
								echo "<span style='color:#cccccc'>н/д</span>";
							} else {
								echo "до $rko_row[account_maintenance_high]<div class='rko_table_mes'>в месяц</div>";
							}
						} else {
							if ($rko_row['account_maintenance_high'] == 0) {
								echo "от $rko_row[account_maintenance_low]<div class='rko_table_mes'>в месяц</div>";
							} else {
								echo "$rko_row[account_maintenance_low]-$rko_row[account_maintenance_high]<div class='rko_table_mes'>в месяц</div>";
							}
						}
					?></td>
					<td class="rko_table_items"><?php
						if ($rko_row['internet_banking_low'] == 0) {
							if ($rko_row['internet_banking_high'] == 0) {
								echo "<span style='color:#cccccc'>н/д</span>";
							} else {
								echo "до $rko_row[internet_banking_high]<div class='rko_table_mes'>в месяц</div>";
							}
						} else {
							if ($rko_row['internet_banking_high'] == 0) {
								echo "от $rko_row[internet_banking_low]<div class='rko_table_mes'>в месяц</div>";
							} else {
								echo "$rko_row[internet_banking_low]-$rko_row[internet_banking_high]<div class='rko_table_mes'>в месяц</div>";
							}
						}
					?></td>
					<td class="rko_table_items rko_left_border"><?php
						if ($rko_row['transfer_low'] == 0) {
							if ($rko_row['transfer_high'] == 0) {
								echo "<span style='color:#cccccc'>н/д</span>";
							} else {
								echo "до $rko_row[transfer_high]";
							}
						} else {
							if ($rko_row['transfer_high'] == 0) {
								echo "от $rko_row[transfer_low]";
							} else {
								echo "$rko_row[transfer_low]-$rko_row[transfer_high]";
							}
						}
					?></td>
					<td class="rko_table_items"><?php
						if ($rko_row['cash_low'] == 0) {
							if ($rko_row['cash_high'] == 0) {
								echo "<span style='color:#cccccc'>н/д</span>";
							} else {
								echo "до $rko_row[cash_high]";
							}
						} else {
							if ($rko_row['cash_high'] == 0) {
								echo "от $rko_row[cash_low]";
							} else {
								echo "$rko_row[cash_low]-$rko_row[cash_high]";
							}
						}
					?></td>
					<td class="rko_table_items"><?php
						if ($rko_row['receiving_cash_low'] == 0) {
							if ($rko_row['receiving_cash_high'] == 0) {
								echo "<span style='color:#cccccc'>н/д</span>";
							} else {
								echo "до $rko_row[receiving_cash_high]";
							}
						} else {
							if ($rko_row['receiving_cash_high'] == 0) {
								echo "от $rko_row[receiving_cash_low]";
							} else {
								echo "$rko_row[receiving_cash_low]-$rko_row[receiving_cash_high]";
							}
						}
					?></td>
				</tr><?php
			}
		?></table>
		<div id="oprosy_table">
			<?php require_once APP."pageskin.php"; ?>
		</div>
	</div><?php
}
else
	echo "<h3 class='gag'>Не найдено</h3>";
?><div class="inside_block">
	<table width="100%" border="0" cellpadding="0" cellspacing="0">
		<tr valign='top'>
			<td width="344" class="inside_info"><?php
				require_once APP."bank_list.php";
				require_once APP."currency_rates.php";
			?></td>
			<td width="646"><?php
				$info = mysql_fetch_array(mysql_query("select * from `info` where `info_id` = '3' limit 1"));
				$text = getRichText($info['text']);
				echo "<h2>$info[header]</h2>";
				echo "<p>$text</p>";
			?></td>
		</tr>
	</table>
</div>
<?php require_once APP.FOOTERFILE; ?>